package cn.example.listviewsimpleadpteer;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener,AbsListView.OnScrollListener{

    private ListView listView;

    private SimpleAdapter simpleAdapter;
    private List<Map<String,Object>> mapList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView=findViewById(R.id.my_listview);
        mapList=new ArrayList<Map<String, Object>>();
        initData();
        /**
         * 1.上下文 2.布局资源文件 3.对应数据操作字段 4.字段对应的布局 组件
         */
        simpleAdapter=new SimpleAdapter(this,mapList,R.layout.item,new String[]{"a","b"},new int[]{R.id.img_view, R.id.tv_view});
        listView.setAdapter(simpleAdapter);

        listView.setOnItemClickListener(this);
        listView.setOnScrollListener(this);
    }

    private void initData() {
        for (int i = 0; i < 5; i++) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("a", R.drawable.a);
            map.put("b","人数"+i);
            mapList.add(map);
        }
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Map<String, Object> map = mapList.get(i);
        Object a = map.get("a");
        Toast.makeText(this, String.valueOf(a),Toast.LENGTH_SHORT).show();
    }

    /**
     * 滚动状态 监听增加内容
     * @param absListView
     * @param i
     */
    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {
        switch (i){
            case SCROLL_STATE_FLING:
                HashMap<String, Object> map = new HashMap<>();
                map.put("a", R.drawable.b);
                map.put("b","增加");
                mapList.add(map);
                //数据更新
                simpleAdapter.notifyDataSetChanged();
                break;
        }

    }

    /**
     * 为本个actiyity 绑定菜单组件
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        this.getMenuInflater().inflate(R.menu.mymenu,menu);
        return true;
    }

    /*
        菜单选项处理
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.it1:
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(intent);
                break;
            case R.id.it2:
                Intent intent2 = new Intent(MainActivity.this, Main3Activity.class);
                startActivity(intent2);
                break;
            case R.id.it3:
                Intent intent3 = new Intent(MainActivity.this, Main4Activity.class);
                startActivity(intent3);
                break;
        }

        return true;
    }

    @Override
    public void onScroll(AbsListView absListView, int i, int i1, int i2) {

    }
}
